package edu.xhu.check.service.impl;

import com.alibaba.fastjson2.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.teaopenapi.models.Config;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xhu.check.mapper.CheckMapper;
import edu.xhu.check.service.CheckService;
import edu.xhu.check.util.Scan;
import edu.xhu.model.common.dtos.ResponseResult;
import edu.xhu.model.common.enums.AppHttpCodeEnum;
import edu.xhu.model.video.pojos.BiliVideo;
import lombok.RequiredArgsConstructor;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.objdetect.HOGDescriptor;
import org.opencv.videoio.VideoCapture;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Random;

@Service
@RequiredArgsConstructor
public class CheckServiceImpl extends ServiceImpl<CheckMapper, BiliVideo> implements CheckService {

    private final OSS ossClient;

    static {
        System.load("E:\\opencv_java490.dll");
    }

    @Override
    public ResponseResult<?> checkVideoTest(String content) {
//        //1.获取视频
//        BiliVideo id = getById(282620654170144768L);
//        if (id == null) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
//        //2.检测文字
//        try {
//            Map<String, String> map = Scan.greenTextScan(content);
//            System.out.println(map.get("reason"));
//            System.out.println(map.get("labels"));
//        } catch (Exception e) {
//            log.error(e.toString());
//        }
//        //3.检测
//        try {
//            Map<String, Object> test = Scan.test();
//            System.out.println(test.get("labels"));
//            System.out.println(test.get("confidence"));
//        } catch (Exception e) {
//            log.error(e.toString());
//        }
        //4.video
        try {
//            Map<String, Object> mp4 = Scan.testMp4();
            //5.result
            Map<String, Object> videoResult = Scan.getVideoResult("vi_f_Fqfga54fv6NZs7e6DKPUGC-1z@9Dt");
            System.out.println(videoResult.get("frameResult"));
            System.out.println(videoResult.get("frameNum"));
        } catch (Exception e) {
            log.error(e.toString());
        }

        return ResponseResult.okResult(true);
    }

    private boolean detectFace(String url) {
        VideoCapture capture = new VideoCapture(url);
        String manFaceDetector = "E:\\haarcascade_frontalface_default.xml";
        String catFaceDetector = "E:\\haarcascade_frontalcatface.xml";
        CascadeClassifier classifier = new CascadeClassifier(catFaceDetector);
        //处理帧数
        Mat frame = new Mat();
        while (capture.read(frame)) {
            //将视频帧转换为灰度图像
            Mat gray = new Mat();
            Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);
            //人脸检测
            MatOfRect foundLocations = new MatOfRect();
            classifier.detectMultiScale(gray, foundLocations, 1.15, 5);
            //判断有没有
            if (foundLocations.toArray().length > 0) {
                // 绘制检测到的人脸
                for (Rect rect : foundLocations.toArray()) {
                    Imgproc.rectangle(gray, new Point(rect.x, rect.y),
                            new Point(rect.x + rect.width, rect.y + rect.height),
                            new Scalar(0, 255, 0));
                }
                // 保存结果图像
                Imgcodecs.imwrite("E:\\" + new Random().nextInt() + "output.jpg", gray);
                capture.release();
                return true;
            }
        }
        return false;
    }

    private boolean detectMan(String url) {
        //2.加载视频文件
        VideoCapture capture = new VideoCapture(url);
        //3.定义HOF人体检测器
        HOGDescriptor hog = new HOGDescriptor();
        hog.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector());
        //4.处理视频帧
        Mat frame = new Mat();
        while (capture.read(frame)) {
            //将视频帧转换为灰度图像
            Mat gray = new Mat();
            Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);
            //人体检测
            MatOfRect foundLocations = new MatOfRect();
            MatOfDouble foundWeights = new MatOfDouble();
            hog.detectMultiScale(gray, foundLocations, foundWeights);
            //如果检测到人体,则判断视频含有不良内容,返回true
            if (foundLocations.toArray().length > 0) {
                capture.release();
                return true;
            }
        }
        capture.release();
        return false;
    }
}
